package com.example.listener;

import com.example.constant.RedisConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.core.StreamOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.stream.StreamListener;
import org.springframework.stereotype.Component;


/**
 * @application:
 * @author: LiYuhang
 * @Date: 2021/5/25 22:56
 * @version: 0.1
 * @Description :
 * @Modified By:
 */
@Component
@Slf4j
public class ListenerMessage1 implements StreamListener<String, MapRecord<String, String, String>> {
    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 消息监听，有数据进来，就会触发onMessage 被动消费
     * @param entries
     */
    @Override
    public void onMessage(MapRecord<String, String, String> entries) {
        StreamOperations streamOperations = redisTemplate.opsForStream();
        Long acknowledge = streamOperations.acknowledge(entries.getStream(),RedisConstant.COMMENT_GROUP,entries.getId());
        log.info("ListenerMessage1 接收到来自redis的消息 stream:{} id:{} ,acknowledge result:{},entries:{}",entries.getStream(),entries.getId(),acknowledge==1,entries);
    }

}
